/** 128 normal
 * 构建哈希表减少查询时间复杂度
 */

function longestConsecutive(nums) {
  const numSet = new Set(nums);
  let maxLength = 0;
  for (const num of numSet) {
    if (!numSet.has(num - 1)) {
      let length = 1;
      let temp = num + 1;
      while (numSet.has(temp)) {
        length++;
        temp = temp + 1;
      }
      if (length > maxLength) {
        maxLength = length;
      }
    }
  }
  return maxLength;
}
console.log(longestConsecutive([100, 4, 200, 1, 3, 2]));
console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]));
